﻿using System;
using System.Linq;
//using Aspose.Email;
//using Aspose.Email.Mail;


namespace msBsc.SvRef
{ 
    public class SendEMail
    {
        public string SendEmailTo(string strReciveNme, string toAddress, string strMailSubject, string strContent)
        {
            try
            {
                ////Declare msg as MailMessage instance
                //MailMessage msg = new MailMessage();

                ////use MailMessage properties like specify sender, recipient and message
                //msg.From = msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailAddress;
                //msg.To = toAddress;
                //msg.Subject = strMailSubject;

                ////Specify HtmlBody
                //msg.HtmlBody = strContent;

                //SmtpClient client1 = new SmtpClient(msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailServer, msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailAddress, msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailPass);

                ////client.SecurityOptions.SecurityMode = SmtpSslSecurityMode.Explicit;

                ////client.EnableSsl = true;

                ////Client will send this message
                //client1.Send(msg);

                ////Show only if message sent successfully 



                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailServer);

                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailAddress, msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailPass);
                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                System.Net.Mail.MailAddress addressFrom = new System.Net.Mail.MailAddress(msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailAddress,msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailSendName  );
                System.Net.Mail.MailAddress addressTo = new System.Net.Mail.MailAddress(toAddress, strReciveNme);

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(addressFrom, addressTo);
                message.Sender = new System.Net.Mail.MailAddress(msBsc.AppLogic.Cfg.SystemSet.oSystemSet.AutoNotifyMailAddress);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Subject = strMailSubject;
                message.Body = strContent;
                client.Send(message);
            }
            catch (Exception err)
            {
                return err.Message;
            }
            return "OK";
        }


        /// <summary>
        /// 打包发送一封邮件，成功发送返回OK,只发不保存记录
        /// </summary>
        /// <param name="strSenderNme"></param>
        /// <param name="strFromAddress"></param>
        /// <param name="strReciveNme"></param>
        /// <param name="toAddress"></param>
        /// <param name="strMailSubject"></param>
        /// <param name="strContent"></param>
        /// <param name="Attachments"></param>
        /// <param name="blnotification"></param>
        /// <returns></returns>
        public string SendEmailTo(string strSenderNme, string strFromAddress, string strReciveNme, string toAddress, string strMailSubject, string strContent, System.Collections.Generic.Dictionary<string, string> Attachments, bool blnotification = true)
        {
            //开始一个非事务范围   
            using (System.Transactions.TransactionScope scope2 = new System.Transactions.TransactionScope(
                System.Transactions.TransactionScopeOption.Suppress))
            {
                try
                {
                     
                    bool blOK = false; 
                    ESmtpMail oEMailMessage = new ESmtpMail(); 

                    oEMailMessage.MailFrom = strFromAddress;
                    oEMailMessage.MailFromName = strSenderNme;

                    if (oEMailMessage.MailFrom == string.Empty)
                    {
                        //默认用系统邮箱
                        oEMailMessage.MailFrom = "jnjyadmin@163.com";
                        oEMailMessage.MailFromName = "管理员";
                    }
                     

                    //统一发送服务器
                    oEMailMessage.MailServerUserName = "jnjyadmin";
                    oEMailMessage.MailServerPassWord = "jnjy11";
                    oEMailMessage.MailDomainServer = "smtp.163.com";
                    oEMailMessage.MailDomainPort = int.Parse("25");
                     

                    if (Attachments != null && Attachments.Count > 0)
                    {
                        oEMailMessage.Attachments=Attachments; 
                    }

                    string[] arrtem32111 = toAddress.Split(new char[] { ';' }).Distinct().ToArray();
                    toAddress = string.Join(";", arrtem32111);
                    int RecipientNum = arrtem32111.Length;
                    for (int i = arrtem32111.Length - 1; i >= 0; i--)
                    {
                        //if (oEMailMessage.strRecipientList == string.Empty)
                        //{
                        //    oEMailMessage.strRecipientList = strAddress;
                        //}
                        //else
                        //{
                        oEMailMessage.AddRecipient(arrtem32111[i]);
                        //}
                        //RecipientNum += 1;
                    }
                    if (RecipientNum == 0)
                    {
                        return "没有收件人地址！";
                    }
                    oEMailMessage.MailBody = strContent;
                    oEMailMessage.MailSMTPvalidate = true;

                    oEMailMessage.IsHTML = true;
                    oEMailMessage.RecipientMaxNum = RecipientNum;
                    oEMailMessage.notification = blnotification;
                    oEMailMessage.MailRecipientName = strReciveNme;
                    oEMailMessage.MailSubject = strMailSubject;
                    blOK = oEMailMessage.Send();
                    if (!blOK && oEMailMessage.ErrorMessage.Trim() != string.Empty)
                    {
                        return (oEMailMessage.ErrorMessage);
                    }
                     
                    return "OK";
                }
                catch (Exception err)
                {
                    return err.Message;
                }
            }
        }

    }
}
